package com.woniuxy.service.impl;

import com.woniuxy.dao.ProductDao;
import com.woniuxy.entity.Product;
import com.woniuxy.entity.utils.ResponseEntity;
import com.woniuxy.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * @Author: 马宇航
 * @Description: 商品服务业务层实现类
 * @DateTime: 22/10/10/0010 16:12
 * @Component: 成都蜗牛学苑
 **/
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductDao productDao;
    @Override
    public int addOne(Product product) {
        productDao.save(product);
        return 1;
    }

    @Override
    public ResponseEntity findByPid(Integer id) {
        Optional<Product> product = productDao.findById(id);
        //判空
        if (product.isPresent()) {
            return new ResponseEntity(product.get());
        }
        return ResponseEntity.ERROR;
    }

    @Override
    public ResponseEntity updateOne(Product product){
        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Product save = productDao.save(product);
        if (save!=null){
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.ERROR;
    }

    @Override
    public ResponseEntity searchPrice(Double floorPrice, Double ceilingPrice) {
        List<Product> all = productDao.findByPriceBetween(floorPrice,ceilingPrice);
        return new ResponseEntity(all);
    }

    @Override
    public ResponseEntity searchProduce(Integer pid, String name, Double price) {

        List<Product> all =  productDao.findByPidOrNameOrPrice(pid, name, price);
        return new ResponseEntity(all);
    }
}
